package com.example.user.customviewdemo.drawing.pathandtext;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by user on 2016/4/22.
 * 绘制各种Path路径和各种样式的文字
 */
public class MyPathAndTextView extends View {
    public MyPathAndTextView(Context context) {
        super(context);
    }

    public MyPathAndTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyPathAndTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * 初始化画笔和画布
         */
        canvas.drawColor(Color.GRAY);
        Paint paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);

        /***********绘制直线路径****************/
        Path path = new Path();
        path.moveTo(10, 10);
        path.lineTo(200, 200);//直线的结束点又是下一条直线的起点
        path.lineTo(200, 10);
        path.close();   //添加close则会将最后一个点和第一个点相连接闭环
        canvas.drawPath(path, paint);

        /*************绘制矩形路径**********************/
        path = new Path();
        path.addRect(250, 10, 450, 200, Path.Direction.CCW);//Path.Direction.CCW表示逆时针方向，CW表示顺时针
        canvas.drawPath(path, paint);//逆时针和顺时针和path图形无关，只是和drawTextOnPath在上面的文字有关
        paint.setTextSize(60);
        paint.setColor(Color.BLACK);
        canvas.drawTextOnPath("我是逆时针矩形路劲", path, 60, 30, paint);

        /*************绘制圆角矩形路径**************/
        path = new Path();
        float[] radii = {50, 20, 50, 20, 10, 20, 10, 20};//必须是八个数字，表示矩形四个圆角对应椭圆的x半径和y半径如{x1,y1,x2,y2,x3,y3,x4,y4};
        RectF roundRectF = new RectF(500, 10, 700, 200);
        path.addRoundRect(roundRectF, radii, Path.Direction.CCW);
        canvas.drawPath(path, paint);

        /**************绘制圆形路径*******************/
        path = new Path();
        path.addCircle(800, 100, 100, Path.Direction.CW);
        canvas.drawPath(path, paint);

        /**************绘制椭圆路径*************/
        RectF ovalRectF = new RectF(10, 200, 300, 400);
        path = new Path();
        path.addOval(ovalRectF, Path.Direction.CW);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        canvas.drawPath(path, paint);

        /**************绘制弧形路径**************/
        RectF arcRectF = new RectF(300, 200, 500, 400);
        path = new Path();
        path.addArc(arcRectF, 0, 90); //绘制弧形没有direction
        paint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path, paint);

        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);//有意思，若果把Style设置成FILL尽然画不出直线path
        path = new Path();
        path.moveTo(0, 410);
        path.lineTo(1000, 410);
        canvas.drawPath(path, paint);
        //canvas.drawLine(0,410,1000,410,paint);但是却可以画直线
        canvas.drawTextOnPath("华丽的分割线", path, 300, 60, paint);


        /**************************绘制文字text******************************************************************************/
        /*总结：文字样式和其他设置api如下
        //样式设置
        paint.setFakeBoldText(true);//设置是否为粗体文字
        paint.setUnderlineText(true);//设置下划线
        paint.setTextSkewX((float) -0.25);//设置字体水平倾斜度，普通斜体字是-0.25
        paint.setStrikeThruText(true);//设置带有删除线效果
        paint.setLetterSpacing(0.5f);//设置字体间距
        paint.setTextAlign(Paint.Align.RIGHT);//该参数表示文字相对于该点来说，默认为LEFT
        //其它设置
        paint.setTextScaleX(2);//只会将水平方向拉伸，高度不会变  */
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);
        paint.setTextSize(80);

        /***************绘图样式设置为填充*************/
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("风萧萧兮易水寒",0,550,paint);

        /**************绘图样式设置为描边************/
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawText("风萧萧兮易水寒",0,650,paint);

        /***************绘图样式设置为描边且填充*************/
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawText("风萧萧兮易水寒",0,750,paint);

        /***************文字设置粗体、下划线、删除线*********/
        paint.setFakeBoldText(true);//设置粗体
        paint.setUnderlineText(true);//设置下划线
        paint.setStrikeThruText(true);//设置删除线
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            paint.setLetterSpacing(0.5f);//设置字体水平间距，按照文字大小的倍数计算，默认为0
        }
        canvas.drawText("风萧萧兮易水寒",0,850,paint);

        /*****************设置文字位置********/
        paint.setTextAlign(Paint.Align.RIGHT);//该参数表示文字相对于该点来说，默认为LEFT
        canvas.drawText("风萧萧兮易水寒",500,950,paint);

        /*****************设置文字倾斜度***************/
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTextSkewX(-0.8f);//正数向左，负数向右
        canvas.drawText("风萧萧兮易水寒",0,1050,paint);

        /******************设置文字水平拉升(只是水平横向进行拉伸，竖直方向不会拉升)************/
        paint.setTextScaleX(0.5f);//按照文字大小的倍数计算，默认为1,小于1表示缩小，大于1，表示放大
        canvas.drawText("风萧萧兮易水寒",0,1150,paint);

        /*************指定文字位置进行绘制*********/
        paint.setTextScaleX(1);
        paint.setTextSkewX(0);
        float[] pos = {0,1250,100,1350,200,1250,300,1350};
        canvas.drawPosText("我爱中国",pos,paint);

        /***************延路劲绘制文字************/
        path = new Path();
        path.addCircle(700,1300,200, Path.Direction.CCW);//延圆环逆时针绘制文字
        canvas.drawPath(path,paint);
        paint.setColor(Color.GREEN);
        canvas.drawTextOnPath("风萧萧兮易水寒",path,0,0,paint);

        /**************字体样式设置****************************/
       /* paint.setTypeface(Typeface typece,int style);
       style:
            Typeface.NORMAL  //正常体
            Typeface.BOLD	 //粗体
            Typeface.ITALIC	 //斜体
             Typeface.BOLD_ITALIC //粗斜体
        Typeface typeface = Typeface.create();
        若要使用自定义的typeface时，需在asset下新建一个Fonts目录，然后将字体文件放到该目录下
        AssetManager mgr=m_context.getAssets();//得到AssetManager
        Typeface typeface=Typeface.createFromAsset(mgr, "fonts/jian_luobo.ttf");//根据路径得到Typeface
        */


    }
}
